<template>
	<view class="Details">
		<scroll-view scroll-y="true" class="content">
			<view style="margin: 10rpx 10rpx 20rpx 10rpx">
				<!-- <ad :unit-id="config.BannerID"></ad> -->
				<ad v-if="showad" unit-id="adunit-48710c49e4042583"></ad>
			</view>
			<uv-line color="#000000"></uv-line>
			<view style="display: flex; flex-direction: column; flex-shrink: 0; margin: 50rpx 10rpx 0 10rpx; min-height: 90vh;">
				<zero-markdown-view :markdown="content"></zero-markdown-view>
			</view>
			<view class="btns" style="margin-bottom: 50rpx;">
				<view
					v-if="gdatas.hint != null && gdatas.hint != '' && gdatas.item.ad == true && gdatas.globalAd == true"
					class="hint">
					{{ gdatas.hint }}
				</view>
				<uv-button v-if="gdatas.item.ad == true" type="success" size="large" class="btn"
					@click="getDownload()">{{gdatas.downloadtext}}</uv-button>
				<uv-button v-else type="primary" class="btn" size="large"
					@click="showVideoAd()">{{gdatas.adbtntext}}</uv-button>
			</view>
			<uv-line color="#000000"></uv-line>

			<view style="margin: 20rpx 10rpx 20rpx 10rpx">
				<!-- <ad-fullscreen-video adpid="1113100813"></ad-fullscreen-video> -->
				<ad v-if="showad" :unit-id="config.FullVideoID" ad-type="video" ad-theme="white"></ad>
			</view>
		</scroll-view>

		<uv-toast ref="toast"></uv-toast>
		<!-- <ad v-if="showad" :unit-id="config.FullVideoID" ad-type="video" ad-theme="white"></ad> -->
		<!-- <ad-rewarded-video adpid="1320436948" ref="videoAd" :loadnext="true" v-slot:default="{loading, error}"
			@load="videoAdLoad" @close="videoAdClose" @error="videoAdError" :url-callback="adcallback">
		</ad-rewarded-video> -->
	</view>
</template>

<script setup>
	import {
		onMounted,
		ref
	} from 'vue';
	import tools from "../../utils/tools.js";
	import gdatas from "../../utils/data.js";
	import config from '../../common/config.js';
	const giteeUrl = ref(config.giteeUrl);
	let content = ref("");
	let videoAd = null
	let isVideoAdLoading = false;
	let showad = ref(false)
	let toast = ref(null)
	// let selectItem = null;
	let adcallback = {
		a: 300,
		b: "400",
	}

	function showToast(params) {
		toast.value.show({
			...params,
			complete() {}
		})
	}

	function getlist() {
		//从码云读取数据
		uni.request({
			url: giteeUrl.value + gdatas.pageUrl,
			success: (res) => {
				content.value = res.data;
			}
		})
	}

	//获取资源链接
	function getDownload() {
		let data = gdatas.linkUrl;
		//判断数据中的encryption是否位true,如果加密就解密
		if (gdatas.encryption == true) {
			//后面为16位的密钥和16位的向量，百度搜索aes cbc加密工具可以对数据中的link进行加密
			data = tools.Decrypt(data, config.passwd, config.iv);
		}
		//复制到粘贴板
		uni.setClipboardData({
			data: data,
		})
	}


	onMounted(() => {

		if (wx.createRewardedVideoAd) {
			videoAd = wx.createRewardedVideoAd({
				adUnitId: 'adunit-d5964ffe414019dc'
			})
			videoAd.onLoad(() => {
				console.log('激励广告加载成功');
				isVideoAdLoading = true;
			})
			videoAd.onError((e) => {
				console.error('激励视频广告加载失败', e)
				// 广告加载失败
				showToast({
					type: 'default',
					message: e,
				})
			})
			videoAd.onClose((e) => {
				// 用户点击了关闭广告
				console.log("激励广告关闭", e);
				if (e && e.isEnded) {
					gdatas.item.ad = true;
					getDownload();
				} else {
					showToast({
						type: 'default',
						message: '激励广告未观看完成',
					})
				}
			})
		}
		showad.value = true

		getlist();
	})

	function showVideoAd() {
		if (isVideoAdLoading) {
			isVideoAdLoading = false;
			videoAd.show();
		} else {
			showToast({
				type: 'default',
				title: '激励广告未加载完成',
				message: "激励广告未加载完成",
			})
		}
	}
</script>

<style>
	page {
		display: flex;
		width: 100%;
		height: 100%;
		background-color: #f2f5f9;
	}
</style>
<style lang="scss" scoped>
	.Details {
		display: flex;
		flex-direction: column;
		height: 100%;
		width: 100%;
		overflow: hidden;
	}

	.content {
		flex-grow: 1;
		overflow: hidden;
	}

	.hint {
		font-size: 0.8rem;
		color: #666;
		width: 100%;
		text-align: center;
		margin: 10rpx auto;
	}

	.btns {
		flex-shrink: 0;
		margin: 50rpx 50rpx 50rpx 50rpx;
	}
</style>